<template>
  <dialog-editor ref="dialog">
    <template v-slot:footer>
      <el-button type="warning" v-if="!R.equals(dataForm['id'], 0)" @click="resetPassword">重置密码</el-button>
    </template>
  </dialog-editor>
</template>

<script>
import {mRequest} from '@/utils/request'
import DialogEditor from '@/components/DialogEditor'
import dialogMixins from '@/utils/commonDialogMixins'

export default {
  mixins: [dialogMixins],
  components: {DialogEditor},
  data() {
    return {
      visible: false,
      dataFormConfig: {
        userId: {
          hidden: true
        },
        // testAttr: {
        //   label: '测试属性',
        //   type: 'cascader',
        //   placeholder: '测试属性',
        //   list: [{
        //     value: 'zhinan',
        //     label: '指南',
        //     children: [{
        //       value: 'shejiyuanze',
        //       label: '设计原则',
        //       children: [{
        //         value: 'yizhi',
        //         label: '一致'
        //       }, {
        //         value: 'fankui',
        //         label: '反馈'
        //       }, {
        //         value: 'xiaolv',
        //         label: '效率'
        //       }, {
        //         value: 'kekong',
        //         label: '可控'
        //       }]
        //     }]
        //   }]
        // },
        username: {
          label: '用户名',
          placeholder: '请输入用户名'
        },
        password: {
          label: '密码',
          inputType: 'password',
          placeholder: '请输入密码',
          reply: () => {
            return this.R.equals(0, this.dataForm['id'])
          }
        },
        confirmPassword: {
          label: '确认密码',
          inputType: 'password',
          placeholder: '确认密码',
          reply: () => {
            return this.R.equals(0, this.dataForm['id'])
          }
        },
        salt: {
          hidden: true
        },
        email: {
          label: '邮箱',
          placeholder: '请输入邮箱'
        },
        mobile: {
          label: '手机号',
          placeholder: '请输入手机号'
        },
        realName: {
          label: '真实姓名',
          placeholder: '请输入真实姓名'
        },
        shortName: {
          label: '简称编号',
          placeholder: '请输入简称编号'
        },
        type: {
          label: '订单显示权限',
          type: 'radio',
          radioList: [
            {text: '无权限', label: 1},
            {text: '显示自己订单', label: 2},
            {text: '显示所有订单', label: 3}
          ],
          default: 2
        },
        roleIdList: {
          label: '角色',
          type: 'checkboxGroup',
          checkList: [],
          labelAttr: 'roleId',
          textAttr: 'roleName',
          default: []
        },
        status: {
          label: '状态',
          type: 'switch',
          activeValue: 1,
          inactiveValue: 0,
          default: 1
        }
      }
    }
  },
  methods: {
    initRoleList() {
      mRequest('systemApi/selectRole', {}, ({data}) => {
        this.dataFormConfig.roleIdList.checkList = this.R.propOr([], 'list', data)
      })
    },
    resetPassword() {
      const userId = this.dataForm.id
      const isSelf = this.R.equals(userId, parseInt(this.$cookie.get('userId'), 10))

      mRequest('systemApi/resetPassword', {userId}, ({data}) => {
        if (this.R.equals(0, data.code)) {
          this.$message({
            message: `重置密码成功${isSelf ? '，请重新登入' : ''}，重置的密码为“ace123!@#$”`,
            type: 'success',
            duration: 1500,
            onClose: () => {
              if (isSelf) {
                this.$cookie.set('token', '')
                this.$router.push({name: 'login'})
              }
            }
          })
        }
      })
    },
    init(id) {
      this.dataForm.id = id || 0
      this.initRoleList()

      this.$refs.dialog.initDialog({
        dataForm: this.dataForm,
        config: this.dataFormConfig,
        dataRule: this.getNeedDataRule('username', 'password', 'confirmPassword', 'email', 'mobile','type'),
        initApi: 'systemApi/userInfo',
        saveApi: 'systemApi/saveUser',
        updateApi: 'systemApi/updateUser',
        objectName: 'user'
      })
    }
  }
}
</script>
